package com.sailing.face.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author :fox
 * @version ：2017年11月7日17:30:25
 */
public class FaceUtil {

	private static Logger logger = LoggerFactory.getLogger(FaceUtil.class);

	public static String postFaceMessage(String json, String url, String sessionId) {
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(url);
		post.setHeader("Content-Type", "application/json");
		//post.setHeader("Cookie", String.format("session_id=%s", sessionId,sessionId));
		post.setHeader("session_id",sessionId);
		post.addHeader("Authorization", "Basic YWRtaW46");
		String result = "";

		try {

			StringEntity s = new StringEntity(json, "utf-8");
			s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
					"application/json"));
			post.setEntity(s);

			// 发送请求
			HttpResponse httpResponse = client.execute(post);

			// 获取响应输入流
			InputStream inStream = httpResponse.getEntity().getContent();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					inStream, "utf-8"));
			StringBuilder strber = new StringBuilder();
			String line = null;
			while ((line = reader.readLine()) != null)
				strber.append(line + "\n");
			inStream.close();

			result = strber.toString();
			System.out.println(result);

			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				System.out.println("请求服务器成功，做相应处理");

			} else {
				System.out.println("请求服务端失败");
			}

		} catch (Exception e) {
			System.out.println("请求异常");
			throw new RuntimeException(e);
		}

		return result;
	}

	/**
	 * 发送get请求
	 * 
	 * @param url
	 *            路径
	 * @return
	 */
	public static Boolean downloadFaceImage(String url, String param,
			String sessionId, String path) {
		URL obj;
		try {
			obj = new URL(url + "?" + param);
			HttpURLConnection con = (HttpURLConnection) obj.openConnection();
			con.setRequestMethod("GET");
			// 添加header
			/*con.setRequestProperty("Cookie", String.format(
					"session_id=%s;face_platform_session_id=%s", sessionId,
					sessionId));*/
			// 请求结果
			int responseCode = con.getResponseCode();
			InputStream in = con.getInputStream();
			byte[] data = null;
			data = new byte[in.available()];
			File file = new File(path);
			FileOutputStream out = new FileOutputStream(path);
			int read = 0;
			while ((read = in.read(data)) != -1) {
				out.write(data, 0, read);
			}
			in.close();
			out.flush();
			out.close();
			return true;
		} catch (Exception e) {
			logger.info("写文件异常");
			e.printStackTrace();
			return false;
		}
	}
}
